package com.yanqu.road.server.cmd.union.cross.parergon;

import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.entity.task.args.union.UnionActivityTaskArgs;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.pb.union.Parergon;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.manger.log.LogMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.property.Property;

import java.math.BigInteger;

@Cmd(code = Protocol.S_UNION_PARERGON_OPERATE_FROM_CROSS, desc = "点击经营")
public class OperateCmdFromCross extends AbstractCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {

        Parergon.OperateRespMsg respMsg = Parergon.OperateRespMsg.parseFrom(packet.getBytes());

        if(respMsg.getRet() == 0){

            //加奖励
            Property property = new Property(GameConfig.GAME_MONEY_SILVER, new BigInteger(respMsg.getRewardMoney()));
            player.getModule(CurrencyModule.class).addCurrency(property, eLogMoneyType.Union, eLogMoneyType.UnionParergonOperate);

            player.notifyListener(eGamePlayerEventType.ParergonOperateTimes.getValue(), respMsg.getFinishTimes());
            player.notifyListener(eGamePlayerEventType.UnionParergonOperateTimes.getValue(), new UnionActivityTaskArgs(UnionActivityTaskArgs.TYPE_ADD_VALUE, respMsg.getFinishTimes(), player.getUserInfo().getUnionUid()));
            player.notifyListener(eGamePlayerEventType.ParergonTotalOperateTimes.getValue(), respMsg.getHistoryTimes());


            //记录日志
            LogMgr.addLogUnionParergon(player.getUserId(), player.getUserInfo().getUnionUid(), respMsg.getParergonId(), respMsg.getProgress(), respMsg.getIsFinish(), respMsg.hasFindBusinessman());
        }

        player.sendPacket(Protocol.U_UNION_PARERGON_OPERATE, respMsg.toBuilder());

    }

    @Override
    public void sendData(GamePlayer player) {

    }
}
